<?php
/**
 * Created by PhpStorm.
 * User: songdewei
 * Date: 2017/10/17
 * Time: 下午3:06
 */

namespace Model\Api;


use Data\Common\CollectionModel;
use Data\Company\CompanyModel;
use Data\Company\ProductModel;
use Data\Post\PostItemModel;

class CollectionController extends BaseController
{
    /**
     *
     */
    public function index(){

    }
    /**
     * 删除收藏
     */
    public function delete(){
        $id = intval($_GET['id']);
        (new CollectionModel())->where(array('uid'=>$this->uid, 'id'=>$id))->delete();
        $this->showAjaxReturn();
    }

    /**
     * 添加收藏
     */
    public function add(){
        $dataid = intval($_GET['dataid']);
        $datatype = trim($_GET['datatype']);
        if ($dataid && $datatype) {
            $collectionModel = new CollectionModel();
            $check = $collectionModel->where(array('uid'=>$this->uid, 'dataid'=>$dataid, 'datatype'=>$datatype))->count();
            if ($check){
                $this->showAjaxReturn();
            }else {
                //商品收藏
                if ($datatype == 'product'){
                    $productModel = new ProductModel();
                    $product = $productModel->where(array('product_id'=>$dataid))->getOne();
                    $collectionModel->data(array(
                        'uid'=>$this->uid,
                        'dataid'=>$dataid,
                        'datatype'=>$datatype,
                        'title'=>$product['title'],
                        'image'=>$product['thumb'],
                        'create_time'=>time()
                    ))->add();
                    $productModel->where(array('itemid'=>$dataid))->data('`collection_num`=`collection_num`+1')->save();
                }

                //店铺收藏
                if ($datatype == 'company'){
                    $companyModel = new CompanyModel();
                    $company = $companyModel->where(array('company_id'=>$dataid))->getOne();
                    $collectionModel->data(array(
                        'uid'=>$this->uid,
                        'dataid'=>$dataid,
                        'datatype'=>$datatype,
                        'title'=>$company['company_name'],
                        'image'=>$company['company_logo'],
                        'create_time'=>time()
                    ))->add();
                    $companyModel->where(array('company_id'=>$dataid))->data('`collection_num`=`collection_num`+1')->save();
                }

                //文章收藏
                if ($datatype == 'article'){
                    $article = (new PostItemModel())->where(array('aid'=>$dataid))->getOne();
                    $collectionModel->data(array(
                        'uid'=>$this->uid,
                        'dataid'=>$dataid,
                        'datatype'=>$datatype,
                        'title'=>$article['title'],
                        'image'=>$article['image'],
                        'create_time'=>time()
                    ))->add();
                }
                $this->showAjaxReturn();
            }
        }else {
            $this->showAjaxError('FAIL', 'invalid_parameter');
        }
    }
}